<template>
  <div class="login">
      <div class="toDay"><span class="back"></span>{{title}}</div>
      <div class="info">
        <p class="title"><input type="text" v-model="name" placeholder="用户名" name=""></p>
        <p class="title"><input @keyup.enter="clickLogin(name,password)" type="password" v-model="password" placeholder="密码" name=""></p>
        <p class="txtle">
          <button @click="clickLogin(name,password)">登录</button>
          <button @click="goRegister">注册</button>
        </p>
      </div>
  </div>
</template>

<script>
import {Get,Post,Put,Del,Login} from '@/config/api.js'


export default {
  name: 'login',
  data () {
    return {
      title:'用户登录',
      name:'',
      password:''
    }
  },

  mounted(){

  },
  methods:{
    goRegister(){
      this.$router.push('/register')
    },
    //点击登录
    clickLogin(name,password){
      if(name!=''&&password!=''){

        let date={
            "grant_type": "password",
            "client_id": "game-ios",
            "client_secret": "0a0e2b7c0f223ff235810cbdc70f1d98",
            "scope": "game_ios",
            "username": name,
            "password": password
        };
        let that=this;

        Login(date).then(function(reg){

          let token=reg.data.access_token
          let arr = token.split('');
          let passToken = "";
          for(let i=0;i<token.length;i++){
            passToken += arr[i].charCodeAt()*3;
            passToken += "/";
          }
          let lastToken=passToken.substring(0,passToken.length-1)
          let d = new Date();
          d.setTime(d.getTime() + (604800000));
          let expires = "expires=" + d.toUTCString();
          document.cookie = "bedLogin" + "=" + lastToken + "; " + expires;
          document.cookie = "bedtoken" + "=" + lastToken + "; " + expires;
          
          that.$store.dispatch("loginsync");
          that.$router.push('/about')
        }).catch(function(err){
          alert(err.response.data.message)
          that.name=''
          that.password=''
        })
        
      }else{
        alert('请输入账号密码！')
      }
      
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
